package com.cv4j.app.fragment;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.ability.CoinsAbility;
import com.cv4j.app.ability.ContourAnalysisAbility;
import com.cv4j.app.ability.DetectQRAbility;
import com.cv4j.app.ability.LineDetectionAbility;
import com.cv4j.app.ability.MorphologyAbility;
import com.cv4j.app.app.BaseFragment;
import com.cv4j.app.utils.ClickUtil;

/**
 * 二值分析
 *
 * @since 2021-05-14
 */
public class BinaryFragment extends BaseFragment {
    private Context mContext;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component root = scatter.parse(ResourceTable.Layout_fragment_binary, container, false);
        mContext = container.getContext();
        initView(root);
        return root;
    }

    private void initView(Component root) {
        root.findComponentById(ResourceTable.Id_text1).setClickedListener(component -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            jumpToAbility(MorphologyAbility.class, (Text) component);
        });
        root.findComponentById(ResourceTable.Id_text2).setClickedListener(component -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            jumpToAbility(CoinsAbility.class, (Text) component);
        });
        root.findComponentById(ResourceTable.Id_text3).setClickedListener(component -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            jumpToAbility(ContourAnalysisAbility.class, (Text) component);
        });
        root.findComponentById(ResourceTable.Id_text4).setClickedListener(component -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            jumpToAbility(LineDetectionAbility.class, (Text) component);
        });
        root.findComponentById(ResourceTable.Id_text5).setClickedListener(component -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            jumpToAbility(DetectQRAbility.class, (Text) component);
        });
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    /**
     * 属性类
     *
     * @param toClass 跳转的类名
     * @param text 文本
     */
    public void jumpToAbility(Class toClass, Text text) {
        if (mContext == null) {
            throw new NullPointerException("you are context is null");
        }
        Intent intent = new Intent();
        intent.setParam("Title", text.getText());
        intent.setOperation(new Intent.OperationBuilder()
            .withBundleName(mContext.getBundleName())
            .withAbilityName(toClass.getName())
            .build());

        // 换成下面context调用
        mContext.startAbility(intent, 0);
    }
}
